<?php
/* ###############################################################################
 *
 * $Id: zipcode.php 65 2015-02-05 19:41:19Z expertsoftware.i@gmail.com $
 *
 * システム名
 *   PHP Web フレームワーク [Ponzu]
 *
 * 機能
 *   郵便番号 ライブラリ
 *
 * バージョン
 *   0.0.0_0
 *
 * ライセンス
 *   LGPL v2.0
 *
 * 履歴
 *   2007/07/01 K.Sonohara
 *     新規作成。
 *
 * ###############################################################################*/

function zipcode($code) {

  if ($code == NULL) return NULL;
  $c = trim($code);
  if ($c == "") return NULL;
  $c = str_replace("'", "", $c);
  $c = str_replace(";", "", $c);
  $c = str_replace('"', "", $c);
  $c = str_replace('%', "", $c);
  $sql = "SELECT * FROM ad_address WHERE zip = '" . $c . "';";
  $db = new SQLite3(DB_HOME . 'data/zipcode.db', SQLITE3_OPEN_READONLY);

  try {
    $result = $db->query($sql);
    if ($result) {
      $row = $result->fetchArray();
      return $row;
    }
  } catch(Exception $e) {
  }
  try {
    if ($db) $db->close();
  } catch(Exception $e) {
    //
  }
}

function zipcodes($code) {
  if ($code == NULL) return NULL;
  $c = trim($code);
//  if ($c == "") return NULL;
  $c = str_replace("'", "", $c);
  $c = str_replace(";", "", $c);
  $c = str_replace('"', "", $c);
  $c = str_replace('%', "", $c);
  $sql = "SELECT * FROM ad_address WHERE zip LIKE '" . $c . "%';";
  $db = new SQLite3(DB_HOME . 'data/zipcode.db', SQLITE3_OPEN_READONLY);

  try {
    $result = $db->query($sql);
    if ($result) {
      $rows = array();
      while ($row = $result->fetchArray()): $rows[] = $row; endwhile;
      return $rows;
    }
  } catch(Exception $e) {
  }
  try {
    if ($db) $db->close();
  } catch(Exception $e) {
    //
  }
}

function zipcodesFromKen($ken) {
  if ($ken == NULL) return NULL;
  $c = trim($ken);
//  if ($c == "") return NULL;
  $c = str_replace("'", "", $c);
  $c = str_replace(";", "", $c);
  $c = str_replace('"', "", $c);
  $c = str_replace('%', "", $c);
  $sql = "SELECT * FROM ad_address WHERE ken_name = '" . $c . "';";
  $db = new SQLite3(DB_HOME . 'data/zipcode.db', SQLITE3_OPEN_READONLY);

  try {
    $result = $db->query($sql);
    if ($result) {
      $rows = array();
      while ($row = $result->fetchArray()): $rows[] = $row; endwhile;
      return $rows;
    }
  } catch(Exception $e) {
  }
  try {
    if ($db) $db->close();
  } catch(Exception $e) {
    //
  }
}

function zipcodeList() {
  $sql = "SELECT * FROM ad_address;";
  $db = new SQLite3(DB_HOME . 'data/zipcode.db', SQLITE3_OPEN_READONLY);

  try {
    $result = $db->query($sql);
    if ($result) {
      $rows = array();
      while ($row = $result->fetchArray()): $rows[] = $row["zip"]; endwhile;
      return $rows;
    }
  } catch(Exception $e) {
  }
  try {
    if ($db) $db->close();
  } catch(Exception $e) {
    //
  }
}

?>
